//rxreducer 

const initialState = {
    data:[],
    detailData:JSON.parse(window.localStorage.getItem('detailData'))||[],
    edits:[]
}

export default (state = initialState, { type, payload }) => {
    const newState =JSON.parse(JSON.stringify(state));
  switch (type) {

  case "GET_LIST":
      newState.data=payload;

    return newState
  case "GET_DATE_LIST":
      if(newState.detailData.lenght <0){
        newState.detailData=payload;
        window.localStorage.setItem('detailData',JSON.stringify(newState.detailData));
      }
      
    return newState
  case "ADD_ADDRESSLIST":
        newState.detailData.push(payload)
        //设置本地存储
        window.localStorage.setItem('detailData',JSON.stringify(newState.detailData));
      return newState
  case "GET_ADDRESS":
    //获取修改的内容
    newState.edits=newState.detailData.filter((item) => {
      return item.id == payload
    })
    return newState    
  case "EDIT_ADDRESS":
   
    newState.detailData.forEach((item,index) => {
      if(item.id === Number(payload.id)){
          console.log(11111)
          newState.detailData[index] = {
            ...payload
          }
        }
    })
    console.log(newState.detailData)
    window.localStorage.setItem('detailData',JSON.stringify(newState.detailData));
    return newState 
  case "DELET_ADDRESS":
    newState.detailData.forEach((item,index)=>{
      if(item.id === payload){
          console.log('2222')
          newState.detailData.splice(index,1)
      }
    })
   console.log(newState.detailData)
    window.localStorage.setItem('detailData',JSON.stringify(newState.detailData));
    return newState;   
  default:
    return newState
  }
}
